1 00:00:00,170 --> 00:00:01,130 Welcome back. 2 00:00:01,130 --> 00:00:07,610 In this lecture we're going to be taking a look at something in the Lua language called meta tables. 3 00:00:07,610 --> 00:00:10,970 This is going to be somewhat more of an advanced topic. 4 00:00:10,970 --> 00:00:14,990 So do not beat yourself up if you aren't able to understand it your first time around. 5 00:00:14,990 --> 00:00:19,580 I didn't understand meta tables when I first got introduced to them as well, so if you didn't understand 6 00:00:19,580 --> 00:00:23,960 the content in this lecture, feel free to continue moving through the course and come back to this 7 00:00:23,960 --> 00:00:25,550 lecture at a later time. 8 00:00:25,550 --> 00:00:27,980 So meta tables, what are they? 9 00:00:27,980 --> 00:00:30,530 Are they a different data type in Lua? 10 00:00:30,770 --> 00:00:33,230 Well, they aren't a different data type. 11 00:00:33,230 --> 00:00:37,190 And in fact meta tables are literally just tables in Lua. 12 00:00:37,190 --> 00:00:41,180 Except these tables have a special job. 13 00:00:41,180 --> 00:00:44,270 Now we already know what a regular table is. 14 00:00:44,270 --> 00:00:49,340 I'll just make a variable called my table here real quick, and we know we can use tables as arrays 15 00:00:49,340 --> 00:00:53,270 or dictionaries so they can store index value pairs or key value pairs. 16 00:00:53,270 --> 00:00:56,690 So here I'll have an array with some numbers inside of it. 17 00:00:56,690 --> 00:01:02,150 And then to create a meta table we need to first create another table. 18 00:01:02,150 --> 00:01:06,170 I'm just going to call this my meta table and set equal to an empty table. 19 00:01:06,170 --> 00:01:09,800 And right now this is just a boring regular old table. 20 00:01:09,800 --> 00:01:17,000 This doesn't become a meta table until we attach this table to another table, like my table, using 21 00:01:17,000 --> 00:01:19,790 a function in Lua called set meta table. 22 00:01:19,790 --> 00:01:23,540 Set meta table is a global function, so we can just type out the name. 23 00:01:23,540 --> 00:01:25,400 And as you can see it pops up right here. 24 00:01:25,400 --> 00:01:27,860 And it wants to take in a table. 25 00:01:27,860 --> 00:01:30,170 And then it needs to take in another table. 26 00:01:30,170 --> 00:01:32,540 That's going to act as the meta table. 27 00:01:32,540 --> 00:01:37,100 And the purpose of this function it says sets the given table's meta table. 28 00:01:37,100 --> 00:01:44,420 The purpose of a meta table is to act as a helper table, to define what the behavior of another table 29 00:01:44,420 --> 00:01:46,760 should be when certain events occur. 30 00:01:46,790 --> 00:01:50,720 A good analogy for a meta table would be a restaurant menu. 31 00:01:50,750 --> 00:01:56,630 Imagine you're looking at the selection of food in the menu, and you want to make a modification to 32 00:01:56,630 --> 00:01:57,740 some kind of menu item. 33 00:01:57,740 --> 00:02:01,400 Maybe you want to add more meat, or maybe you want to add extra spice. 34 00:02:01,400 --> 00:02:07,280 Whatever the case may be, that's a specific event occurring where you want to modify something already 35 00:02:07,280 --> 00:02:09,080 predefined in the menu. 36 00:02:09,080 --> 00:02:14,000 Now let's say there's a small section in the menu that details exactly what happens when you make a 37 00:02:14,000 --> 00:02:20,300 modification to a menu item, like if you wanted to add extra meat, that's going to be $4 extra, or 38 00:02:20,300 --> 00:02:23,420 if you wanted to add extra spice, that's going to be a buck 50 extra. 39 00:02:23,420 --> 00:02:29,150 That small section in the menu that tells you what exactly will happen when you try to modify the menu 40 00:02:29,150 --> 00:02:31,010 is like a meta table in Lua. 41 00:02:31,010 --> 00:02:34,040 So let me set this meta table on my table here. 42 00:02:34,040 --> 00:02:38,210 We need to pass my table first, and then we can pass my meta table and boom! 43 00:02:38,210 --> 00:02:46,040 Now right after line five, my meta table is officially a meta table because it is attached to my table 44 00:02:46,040 --> 00:02:48,950 fulfilling the purpose or the role of a meta table. 45 00:02:48,950 --> 00:02:55,040 Going back to our menu example, let's say we wanted to be able to add a number into our table. 46 00:02:55,040 --> 00:02:56,960 So let's say we created a new variable. 47 00:02:56,960 --> 00:03:03,050 We call it other table and we want it to be equal to my table plus the value of three. 48 00:03:03,050 --> 00:03:04,010 By default. 49 00:03:04,010 --> 00:03:06,890 The Lua interpreter has no idea how to do this. 50 00:03:06,890 --> 00:03:11,960 It doesn't know how to add this number into the table using this addition operator. 51 00:03:11,960 --> 00:03:16,610 And this same thing could happen with a menu that has no section inside of it. 52 00:03:16,610 --> 00:03:22,580 Defining what should happen when, for example, someone requests extra meat, how much extra money 53 00:03:22,580 --> 00:03:25,070 should the restaurant charge for that customer? 54 00:03:25,070 --> 00:03:31,700 Now, wouldn't it be neat if we were able to define what exactly we want to happen when we, for example, 55 00:03:31,700 --> 00:03:38,270 try to add a number into our table and then this is where we can use our meta table for that task. 56 00:03:38,270 --> 00:03:45,260 The purpose of the meta table is to store functions that get executed internally by the Lua interpreter 57 00:03:45,260 --> 00:03:49,550 when specific events occur, like when we try to add a number to our table. 58 00:03:49,550 --> 00:03:57,500 The creators of Lua defined a set of identifiers or keys that we must use inside of the meta table, 59 00:03:57,500 --> 00:04:03,110 and we can associate a function with those keys and those key value pairs that get executed during a 60 00:04:03,110 --> 00:04:09,860 certain situation are the meta methods, and the term or phrase method is just another word for function, 61 00:04:09,860 --> 00:04:14,240 but methods usually indicate that the function belongs to something. 62 00:04:14,240 --> 00:04:21,050 In this case, if we create any meta methods or functions for our meta table, those functions belong 63 00:04:21,080 --> 00:04:24,770 to the meta table, so technically they are a method. 64 00:04:24,770 --> 00:04:30,260 Not all meta methods have to be functions, but most of the time they will be functions. 65 00:04:30,260 --> 00:04:32,840 If we take a look at the robotics documentation. 66 00:04:32,840 --> 00:04:39,350 As you can see, there is a huge list of all of these different predefined meta methods that exist that 67 00:04:39,350 --> 00:04:41,420 we can use for meta tables. 68 00:04:41,420 --> 00:04:47,300 For example, we can define what we want to happen when we use the addition operator on our table. 69 00:04:47,300 --> 00:04:51,260 And that's defined by the special key right here called underscore underscore add. 70 00:04:51,290 --> 00:04:57,110 There's also a meta method for when we want to use the subtraction operator multiplication division. 71 00:04:57,110 --> 00:04:59,660 If you use modulus the exponentiation. 72 00:05:00,390 --> 00:05:02,820 Even two string and a whole bunch more in here. 73 00:05:02,820 --> 00:05:08,250 And all of these different meta methods will get executed or use when these specific events happen, 74 00:05:08,250 --> 00:05:10,380 like when we use the addition operator or whatever. 75 00:05:10,380 --> 00:05:16,710 So let's go ahead and use the underscore underscore add meta method to solve our issue of trying to 76 00:05:16,710 --> 00:05:19,290 add that number of three into our table. 77 00:05:19,290 --> 00:05:23,040 And as you can see right here, it says that it's going to be passed a table. 78 00:05:23,040 --> 00:05:26,460 And the value that we're attempting to add into the table. 79 00:05:26,460 --> 00:05:28,410 So this is going to have to be a function. 80 00:05:28,410 --> 00:05:33,810 So back in studio we can go into our meta table or the table acting as our meta table. 81 00:05:33,810 --> 00:05:35,850 And we can create a new key inside of it. 82 00:05:35,850 --> 00:05:38,160 And we need to call it underscore underscore add. 83 00:05:38,190 --> 00:05:43,860 The reason we have to do this is because these are just the names that the creators of Lua have defined 84 00:05:43,860 --> 00:05:44,700 for meta methods. 85 00:05:44,700 --> 00:05:48,600 And then we can set this key equal to a new function. 86 00:05:48,600 --> 00:05:52,170 And like we saw in the documentation, it's going to be passed the table. 87 00:05:52,170 --> 00:05:54,000 So we could call this our table. 88 00:05:54,000 --> 00:05:57,420 And then the value that we're attempting to add into our table. 89 00:05:57,420 --> 00:05:58,650 So we'll just call it value. 90 00:05:58,650 --> 00:06:04,170 Now inside of this function is where we have the ability to define exactly what we want to happen when 91 00:06:04,170 --> 00:06:07,620 we try to add, for example, the number three into our table. 92 00:06:07,620 --> 00:06:12,720 So what I'm going to do is I'm just going to create a simple for loop that's going to loop through every 93 00:06:12,720 --> 00:06:16,920 index value pair inside of our table. 94 00:06:17,740 --> 00:06:23,590 And what we want to do is we just want to go ahead and update inside of our table at the current index, 95 00:06:23,590 --> 00:06:30,040 we want to set it equal to its current value plus the value we're attempting to add into the table. 96 00:06:30,040 --> 00:06:35,440 And then this method expects us to return a value back out of the function. 97 00:06:35,440 --> 00:06:41,140 So that way, once this addition operation down here completes, we can store the result of this operation 98 00:06:41,140 --> 00:06:43,720 inside of our variable right here called other table. 99 00:06:43,720 --> 00:06:50,020 So let's go ahead and just return our table back to where this function is called which is down here. 100 00:06:50,020 --> 00:06:53,080 So let's print other table into the console. 101 00:06:53,080 --> 00:06:56,440 And then let's run all of this code inside of the command line. 102 00:06:56,440 --> 00:06:58,570 And let's see what output we get. 103 00:06:58,570 --> 00:06:58,930 All right. 104 00:06:58,930 --> 00:06:59,920 It printed a table. 105 00:06:59,920 --> 00:07:05,320 And as you can see it has printed our original table which contained the values of one, two and three. 106 00:07:05,320 --> 00:07:09,940 But now it has added the value of three to every single index value pair. 107 00:07:09,940 --> 00:07:14,620 So one became four, two became five, and three became six. 108 00:07:14,620 --> 00:07:15,430 Very cool. 109 00:07:15,430 --> 00:07:24,520 Now something very important to note is that most of the time you will not want to directly alter the 110 00:07:24,520 --> 00:07:27,400 table passed to our meta method. 111 00:07:27,400 --> 00:07:33,460 Instead, what we want to do is we want to create a new table, and we want this table to observe the 112 00:07:33,460 --> 00:07:38,380 changes without affecting the original table passed to the meta method. 113 00:07:38,380 --> 00:07:39,790 Why do we want to do this? 114 00:07:39,790 --> 00:07:45,010 Well, it's because we don't want our function to have any what we call side effects. 115 00:07:45,010 --> 00:07:49,810 So what I'm going to do here is I'm actually going to print other table, but then I also want to print 116 00:07:49,810 --> 00:07:50,710 my table. 117 00:07:50,710 --> 00:07:51,250 Afterwards. 118 00:07:51,250 --> 00:07:53,620 We're going to print both of these tables out into the console. 119 00:07:55,150 --> 00:07:59,980 And if we run the code and we take a look at both tables, what you're going to notice is that hold 120 00:07:59,980 --> 00:08:00,430 on. 121 00:08:00,430 --> 00:08:03,340 Both tables are exactly the same. 122 00:08:03,340 --> 00:08:05,560 They both have the values of four, five and six. 123 00:08:05,560 --> 00:08:11,530 Even my table had the value of three added to it, and both of them contain four, five and six. 124 00:08:11,530 --> 00:08:12,430 What is happening? 125 00:08:12,430 --> 00:08:19,150 Well, the issue that's happening is that because we are performing these alterations to the table directly 126 00:08:19,150 --> 00:08:24,700 on the table, passed to the meta method, we are inadvertently manipulating the table stored in my 127 00:08:24,700 --> 00:08:28,330 table, and then we're storing that exact same table in the other table. 128 00:08:28,330 --> 00:08:34,030 So what this means is that other table and my table both refer to the exact same table in memory. 129 00:08:34,030 --> 00:08:34,810 It's the same thing. 130 00:08:34,810 --> 00:08:38,410 If I manipulated other table, it would also affect my table. 131 00:08:38,410 --> 00:08:42,280 And if I were to manipulate my table it would also affect other table. 132 00:08:42,280 --> 00:08:47,470 So let's go ahead and manipulate my table by using some kind of function in the table library. 133 00:08:47,470 --> 00:08:52,720 Like for example, let's clear out all of the index value pairs inside of my table. 134 00:08:52,720 --> 00:08:58,030 Do you think it's also going to affect the other table or the table stored in other table? 135 00:08:58,030 --> 00:08:58,990 Well, let's find out. 136 00:08:58,990 --> 00:09:01,750 Let's print both of these tables again in the console. 137 00:09:01,750 --> 00:09:03,670 And then let's run this code. 138 00:09:03,670 --> 00:09:05,260 And what do you notice. 139 00:09:05,260 --> 00:09:09,280 Well both of our tables were filled with index value pairs. 140 00:09:09,280 --> 00:09:11,680 But now both tables have been cleared out. 141 00:09:11,680 --> 00:09:18,400 And that's because as I stated, both of these variables are referring to the exact same table in memory. 142 00:09:18,400 --> 00:09:25,600 So directly manipulating a table passed to our function is a big no no, and it can cause a lot of confusion 143 00:09:25,600 --> 00:09:27,430 and side effects in your code. 144 00:09:27,430 --> 00:09:33,460 So what we want to do is we want to basically promise that we are not going to modify or manipulate 145 00:09:33,460 --> 00:09:38,140 our table, and we're only going to use this as basically a read only table. 146 00:09:38,140 --> 00:09:42,850 And instead we want to create a new table in here and return that new table. 147 00:09:42,850 --> 00:09:43,540 This way. 148 00:09:43,540 --> 00:09:49,900 We're kind of closely upholding a concept in functional programming called pure functions. 149 00:09:49,900 --> 00:09:56,200 And basically a pure function should take an input like we're taking an input here and it should give 150 00:09:56,200 --> 00:09:56,920 an output. 151 00:09:56,920 --> 00:10:01,480 And if the input is the same, the output should also be the same. 152 00:10:01,480 --> 00:10:03,940 The function should not cause any side effects. 153 00:10:03,940 --> 00:10:09,580 So we should not be manipulating the values passed to our function, and the function should also not 154 00:10:09,580 --> 00:10:12,340 access any data outside of itself. 155 00:10:12,340 --> 00:10:18,160 The only data are pure functions should be able to access is whatever values are passed as arguments 156 00:10:18,160 --> 00:10:19,210 to our functions. 157 00:10:19,210 --> 00:10:22,900 So let's go ahead and change the code inside of our add meta method. 158 00:10:22,900 --> 00:10:26,800 And instead what we're going to do is we're going to create a brand new table. 159 00:10:26,800 --> 00:10:28,330 Let's just call it result. 160 00:10:28,330 --> 00:10:29,680 There's our new table. 161 00:10:29,680 --> 00:10:35,620 And instead of manipulating our table and the values inside of our table, let's go ahead and swap this 162 00:10:35,620 --> 00:10:37,750 out with our new result table. 163 00:10:37,750 --> 00:10:44,770 So we're going to be storing still at the same index, the value that is inside of our table plus the 164 00:10:44,770 --> 00:10:45,940 new value we want to add. 165 00:10:45,940 --> 00:10:48,970 And then we can go ahead and return this new result table. 166 00:10:48,970 --> 00:10:52,990 So let's go ahead and copy all of this code again and run this brand new code. 167 00:10:52,990 --> 00:10:54,640 And let's see what outputs we get. 168 00:10:54,640 --> 00:10:57,430 Well let's take a look. 169 00:10:57,430 --> 00:11:03,820 If you noticed that issue or that side effect that our add meta method was causing in our code has now 170 00:11:03,820 --> 00:11:04,480 disappeared. 171 00:11:04,480 --> 00:11:08,200 So now we have two unique tables in different places in memory. 172 00:11:08,200 --> 00:11:12,670 We have the new table that had the value of three added to all the index value pairs. 173 00:11:12,670 --> 00:11:16,720 And we still also have our original table with the values of one, two and three. 174 00:11:16,720 --> 00:11:23,560 And even after we cleared all of the values inside of my table, all of the index value pairs in the 175 00:11:23,560 --> 00:11:26,110 other table still exist, which is great. 176 00:11:26,110 --> 00:11:31,750 Back in the documentation, let's go ahead and look at another meta method and see if we can implement 177 00:11:31,750 --> 00:11:33,700 that inside of our meta table. 178 00:11:33,700 --> 00:11:36,280 In fact, let's go ahead and do the subtraction operator. 179 00:11:36,280 --> 00:11:42,130 So as a challenge for you, I would like you to implement the subtraction meta method inside of your 180 00:11:42,130 --> 00:11:42,700 meta table. 181 00:11:42,700 --> 00:11:47,710 Take a few minutes to do that, and then I will go ahead and show you my solution step by step afterwards. 182 00:11:47,710 --> 00:11:52,330 So pause the lecture now and I'll see you in a little bit okay. 183 00:11:52,330 --> 00:11:56,980 So let's go ahead and take a look at how we can add functionality for the subtraction meta method. 184 00:11:56,980 --> 00:12:02,290 It's actually quite simple because we basically already have the logic written out for our subtraction 185 00:12:02,290 --> 00:12:03,880 meta method right here. 186 00:12:03,880 --> 00:12:08,590 The only thing we need to do is just replace this addition operator with the minus operator. 187 00:12:08,590 --> 00:12:14,710 So let's just copy this function here, and then let's create a new key which is going to be underscore 188 00:12:14,710 --> 00:12:16,870 underscore sub for subtraction. 189 00:12:16,870 --> 00:12:19,630 And we're going to set it equal to the same function. 190 00:12:19,630 --> 00:12:24,460 But instead let's swap this addition operator for a subtraction operator. 191 00:12:24,460 --> 00:12:27,850 Now we should be able to perform subtraction on our table. 192 00:12:27,850 --> 00:12:31,420 So let's go ahead and swap this from an addition to a minus symbol. 193 00:12:31,420 --> 00:12:36,220 And let's run this new code and see if the subtraction function worked. 194 00:12:36,700 --> 00:12:39,730 So if we execute the code let's go ahead and take a look at the output. 195 00:12:39,730 --> 00:12:41,140 And look at that. 196 00:12:41,140 --> 00:12:46,630 We have our new table that had the value of three subtracted from every single index value pair. 197 00:12:46,630 --> 00:12:51,760 So one became negative two, two became negative one and three became zero. 198 00:12:51,760 --> 00:12:52,480 Very cool. 199 00:12:52,480 --> 00:12:54,610 Another thing I would like to highlight that's important. 200 00:12:54,900 --> 00:12:55,800 For you to know. 201 00:12:55,800 --> 00:12:57,840 So that way you don't make the same mistake. 202 00:12:57,840 --> 00:13:04,770 Is that after we have returned this new result table from our meta methods, you're going to notice 203 00:13:04,770 --> 00:13:08,580 that this new result table doesn't have a meta table attached to it. 204 00:13:08,580 --> 00:13:15,480 So that means, for example, the table that is stored in other table is no longer able to have any 205 00:13:15,480 --> 00:13:20,370 of these other operations performed on it, like addition and subtraction, because it's missing that 206 00:13:20,370 --> 00:13:20,910 meta table. 207 00:13:20,910 --> 00:13:23,700 We didn't attach it in any of these functions here. 208 00:13:23,700 --> 00:13:29,490 So to demonstrate this, if I were to set other table equal to itself, and let's say we added the value 209 00:13:29,490 --> 00:13:35,670 of five to it and we tried to execute this code, you're going to see that we've run into an error. 210 00:13:35,670 --> 00:13:39,870 And it says attempt to perform arithmetic addition on table and number. 211 00:13:39,870 --> 00:13:46,080 And this is the interpreter telling you or it's screaming at you saying, hey, I don't know how to 212 00:13:46,080 --> 00:13:46,740 do this. 213 00:13:46,740 --> 00:13:48,930 And that's because we lost the meta table. 214 00:13:48,930 --> 00:13:55,320 So to fix this issue, we just need to simply reattach the meta table back on to our new result table. 215 00:13:55,320 --> 00:14:01,410 So an easy way for us to do that is to just call set meta table, pass our result table here, and then 216 00:14:01,410 --> 00:14:04,560 pass our meta table which is my meta table. 217 00:14:04,560 --> 00:14:07,500 And uh oh what's going on here? 218 00:14:07,500 --> 00:14:08,940 Unknown global. 219 00:14:08,940 --> 00:14:09,960 My meta table. 220 00:14:09,960 --> 00:14:16,980 Well, the issue is right now is because my meta table is a local variable, we are not able to access 221 00:14:16,980 --> 00:14:22,320 or refer to my meta table within the table being assigned to my meta table. 222 00:14:22,320 --> 00:14:29,580 So to fix this, we can just simply declare my meta table up here and initialize it with no value. 223 00:14:29,580 --> 00:14:35,850 And then afterwards we can go ahead and assign the value to the already existing my meta table variable. 224 00:14:35,850 --> 00:14:41,280 And as you can see now that has fixed the issue because the interpreter now knows what variable we're 225 00:14:41,280 --> 00:14:45,780 referring to, and we can go ahead and do this same operation in our addition meta method. 226 00:14:45,780 --> 00:14:48,270 So let's go ahead and paste that inside of there as well. 227 00:14:48,270 --> 00:14:50,220 And now if we run this new code. 228 00:14:51,730 --> 00:14:58,660 You can see that our error has disappeared because the new table stored inside of other table has our 229 00:14:58,660 --> 00:15:03,070 meta table attached to it, and it knows how to do the addition operation. 230 00:15:03,100 --> 00:15:10,720 Another commonly used meta method is called underscore underscore index, and this method is used when 231 00:15:10,720 --> 00:15:15,850 you attempt to index or access a key in your table that does not exist. 232 00:15:15,850 --> 00:15:20,320 If it does not exist, then what the lure interpreter is going to do is it's going to check to see if 233 00:15:20,320 --> 00:15:25,300 your table has a meta table, and if that meta table has this meta method. 234 00:15:25,300 --> 00:15:30,790 If it does have this meta method, then the interpreter will use it to get a potential value for the 235 00:15:30,790 --> 00:15:33,790 key that we're trying to index for in the table. 236 00:15:33,790 --> 00:15:38,680 As you can see, the description says it is fired when table when you try to index with something is 237 00:15:38,680 --> 00:15:39,310 indexed. 238 00:15:39,310 --> 00:15:46,390 If that index is nil, it says it can also be set to a table, in which case that table will be indexed 239 00:15:46,390 --> 00:15:46,930 instead. 240 00:15:46,930 --> 00:15:52,600 So you can set the underscore underscore index meta method to be either a function or you can also set 241 00:15:52,600 --> 00:15:54,130 it to be a table. 242 00:15:54,130 --> 00:16:02,770 So back inside of studio let's say we tried to print whatever value is stored in my table at the index 243 00:16:02,770 --> 00:16:03,430 of four. 244 00:16:03,430 --> 00:16:06,700 Now obviously we only have three elements in the table. 245 00:16:06,700 --> 00:16:09,100 So this should print nil into the console. 246 00:16:09,760 --> 00:16:12,910 So if we execute the code as you can see it printed nil. 247 00:16:12,940 --> 00:16:19,030 But let's say we wanted to be able to maybe have some kind of table store, a bunch of key value pairs 248 00:16:19,030 --> 00:16:21,970 that could be shared across multiple different tables. 249 00:16:21,970 --> 00:16:26,350 So it can kind of act as like, I don't know, a table full of constants or something. 250 00:16:26,350 --> 00:16:30,190 This is where we can use the underscore underscore index meta method. 251 00:16:30,190 --> 00:16:33,820 And first to demonstrate how it works, I'm just going to set it equal to a function. 252 00:16:33,820 --> 00:16:38,800 And this function is going to be passed the key that we're trying to index into our table. 253 00:16:38,800 --> 00:16:41,380 So it's going to be first past our table. 254 00:16:41,380 --> 00:16:42,850 So I'll call it our table. 255 00:16:42,850 --> 00:16:46,870 And then the key that we're attempting to index in our table. 256 00:16:46,870 --> 00:16:50,410 And let's just go ahead and print out that key into the console. 257 00:16:50,410 --> 00:16:57,430 So now what should happen here is we should also get uh, four printed out in the console as well as 258 00:16:57,430 --> 00:16:57,910 nil. 259 00:16:57,910 --> 00:16:59,920 So let's run this new code. 260 00:17:00,730 --> 00:17:05,620 And as you can see it printed four, because it printed our key that we tried to access our table with. 261 00:17:05,620 --> 00:17:10,750 And then it also printed nil because our function didn't return anything, I could return some kind 262 00:17:10,750 --> 00:17:11,740 of constant value here. 263 00:17:11,740 --> 00:17:13,480 Like if I return five. 264 00:17:13,480 --> 00:17:19,240 What's going to happen now is that if I were to run this code, you're going to see that instead of 265 00:17:19,240 --> 00:17:24,400 printing nil, it now prints five, because that's the new value that we're saying, hey, use this 266 00:17:24,400 --> 00:17:30,430 value when you try to index your table with an index that does not exist. 267 00:17:30,430 --> 00:17:36,760 And as the documentation stated, we could also set underscore underscore index to be a table. 268 00:17:36,760 --> 00:17:41,770 So what I'm going to do real quick here is I'm just going to get rid of that line. 269 00:17:41,770 --> 00:17:48,760 And then let's try to index my table with a string of underscore underscore let's say add. 270 00:17:48,760 --> 00:17:54,850 And if we run this code you're going to see that obviously it printed nil because we do not have access 271 00:17:54,850 --> 00:17:55,690 to this key. 272 00:17:55,690 --> 00:18:01,780 If we think about our table here right there is no key value pair inside of the table called underscore 273 00:18:01,780 --> 00:18:02,830 underscore add. 274 00:18:02,830 --> 00:18:10,120 However, what if we wanted to be able to access this meta method that's stored in the meta table attached 275 00:18:10,120 --> 00:18:11,230 to my table? 276 00:18:11,230 --> 00:18:17,020 Well, what we can do is we can set underscore underscore index to our meta table. 277 00:18:17,020 --> 00:18:18,550 So my meta table. 278 00:18:19,030 --> 00:18:25,750 And now what's going to happen is that anytime we try to index my table with a key that does not exist, 279 00:18:25,750 --> 00:18:30,700 it's now going to search through this table instead and see if it can find that key. 280 00:18:30,700 --> 00:18:35,320 And of course we have the underscore underscore add key right here. 281 00:18:35,320 --> 00:18:41,470 So now what should happen is we should have this print out a function into the console instead. 282 00:18:41,470 --> 00:18:43,840 And unfortunately actually that's still printed nil. 283 00:18:43,840 --> 00:18:45,790 So let me try something a little bit different here. 284 00:18:45,790 --> 00:18:48,940 It might be because I'm declaring it right inside of the table. 285 00:18:48,940 --> 00:18:54,100 So let me actually delete that and move it outside of my meta table. 286 00:18:54,100 --> 00:18:59,320 So we could do my meta table dot underscore underscore index and set it equal to itself. 287 00:18:59,320 --> 00:19:02,320 And let's try to see if it'll work that way. 288 00:19:02,320 --> 00:19:04,060 So we'll run the code okay. 289 00:19:04,060 --> 00:19:04,420 Perfect. 290 00:19:04,420 --> 00:19:05,560 Yeah it works this way. 291 00:19:05,560 --> 00:19:11,170 As you can see our function has printed out inside of the console why it didn't work the first time. 292 00:19:11,170 --> 00:19:16,000 I think it might have to do to the fact that it might be some kind of, like, endless cycle, because, 293 00:19:16,000 --> 00:19:20,560 I mean, if you kind of think about it, if you're setting underscore, underscore, index or a value 294 00:19:20,560 --> 00:19:25,420 in here equal to my meta table, which is also the value we're trying to set to the variable itself, 295 00:19:25,420 --> 00:19:26,650 it kind of goes in a loop. 296 00:19:27,160 --> 00:19:29,110 So I think that's why it wasn't working. 297 00:19:29,110 --> 00:19:31,420 But as you can see we declared it outside. 298 00:19:31,420 --> 00:19:34,270 After we initialized my meta table with this table. 299 00:19:34,270 --> 00:19:39,010 We just added this new underscore underscore index key to my meta table. 300 00:19:39,010 --> 00:19:40,960 Set it equal to itself. 301 00:19:40,960 --> 00:19:45,940 And then now we were able to access the functions inside of the meta table. 302 00:19:45,940 --> 00:19:48,400 And then we could also define other keys in our meta table. 303 00:19:48,400 --> 00:19:50,140 Like we could say hello. 304 00:19:50,330 --> 00:19:54,680 And set it equal to a value of five, and we should be able to access that key. 305 00:19:55,190 --> 00:19:56,180 So if I do dot. 306 00:19:56,180 --> 00:19:56,870 Hello. 307 00:19:56,900 --> 00:19:59,480 As you can see it's actually also auto filling for us two. 308 00:19:59,510 --> 00:20:05,150 So the linter in Lua or basically the intellisense that helps you auto fill your code. 309 00:20:05,150 --> 00:20:11,510 It knows that my table has my meta table attached to it, and it also knows that my meta table has an 310 00:20:11,510 --> 00:20:14,120 underscore underscore index meta method. 311 00:20:14,120 --> 00:20:21,260 And that meta method is set to a table that contains our key, which is why we are able to auto fill 312 00:20:21,260 --> 00:20:22,070 right here. 313 00:20:22,130 --> 00:20:26,390 And if we run this code, as you can see, it's going to print out the value of five, which is very 314 00:20:26,390 --> 00:20:26,780 cool. 315 00:20:26,780 --> 00:20:30,950 So the link to all of these meta methods will be attached to this lecture. 316 00:20:30,950 --> 00:20:35,420 And what I would like for you to do is I would like for you to go to that link, take a look at all 317 00:20:35,420 --> 00:20:39,620 these meta methods and pull out a couple that you want to experiment with. 318 00:20:39,620 --> 00:20:40,460 Have fun with it. 319 00:20:40,460 --> 00:20:44,060 Try to see how they work and see how you can use them in your meta table. 320 00:20:44,060 --> 00:20:44,780 Pause the video. 321 00:20:44,780 --> 00:20:49,520 Now take a few minutes of your time to mess around with these different meta methods, because experimentation 322 00:20:49,520 --> 00:20:54,890 and practicing messing around with them is the best and fastest way you're going to be able to learn 323 00:20:54,890 --> 00:20:56,480 and pick up this new concept. 324 00:20:56,480 --> 00:21:02,300 So yeah, meta tables, they are not as scary as some people make them out to be. 325 00:21:02,300 --> 00:21:08,360 They're simply helper tables that define the behavior for other tables when certain situations occur. 326 00:21:08,360 --> 00:21:15,080 Once more, if you had a hard time understanding the information in this lecture, feel free to attempt 327 00:21:15,080 --> 00:21:19,130 the Meta Tables quiz and then just move on through the course. 328 00:21:19,130 --> 00:21:21,770 You can always come back to this lecture at a later date. 329 00:21:21,770 --> 00:21:27,860 In the next lecture, we're going to continue expanding upon meta tables by using them to imitate a 330 00:21:27,860 --> 00:21:32,420 paradigm in programming called object oriented programming. 331 00:21:32,420 --> 00:21:37,850 This is somewhat of another advanced topic, so don't feel discouraged if you don't understand it at 332 00:21:37,850 --> 00:21:38,450 first. 333 00:21:38,450 --> 00:21:41,780 With all of that said, I'll see you in the next lecture.